// 公共登录
export default function login() {
	let width,left,duanWidth,duanLeft
	uni.getSystemInfo({
		success: (res) => {
			if (res.windowWidth <= 375) {
				width = res.windowWidth - 50
				left = 50 / 2
				duanWidth = res.windowWidth - 89
				duanLeft = duanWidth / 2
			} else if (res.windowWidth <= 414) {
				width = res.windowWidth - 50
				left = 50 / 2
				duanWidth = res.windowWidth - 89
				duanLeft = duanWidth / 2
			}
		}
	})
	console.log(width,left,duanWidth,duanLeft)
	// #ifdef APP-PLUS
	return new Promise((resolve, reject) => {
		
		const jv = uni.requireNativePlugin('JG-JVerification')
		jv.setDebugMode(true) // 开启 debug 模式，请在初始化前调用
		jv.init({
			timeout:7000,
			isProduction:false,
		},result=>{
			// 自定义页面的点击事件
			console.log(result)
			let code = result.code
			let desc = result.content
			// 判断当前网络环境是否可以发起认证
			jv.checkVerifyEnable(result=>{
				let isSupport = result.enable;
				console.log(isSupport)
				if (!isSupport) {
					uni.showToast({title: '不支持一键登入',icon: 'none'})
					resolve('fail')
				}
				if (code === 8000 && isSupport) {
					jv.isInitSuccess(result=>{
						let isSucc = result.enable;
						console.log(result)
						if (isSucc) {
							jv.preLogin(7000,result=>{
								let code = result.code;
								let token = result.content;
								console.log(result)
								// 预登陆失败 记得判断
								if (code === 7000) {
									if(uni.getSystemInfoSync().platform == "ios") {
										jv.addCustomViewsClickCallback(id=>{
											console.log(id)
											if (id == 1) {
												// 隐藏登录页
												jv.dismissLoginAuth(false, res=>{
													//授权页隐藏完成
													uni.navigateTo({url: '/pages/login/login'})
												})
											} else if (id == 2) {
												jv.dismissLoginAuth(false, res=>{
													uni.startPullDownRefresh()
												})
											} else if(id == 3) {
												jv.dismissLoginAuth(false, res=>{
													uni.getProvider({
														service: 'oauth',
														success: (res)=> {
															console.log(res.provider);
															//支持微信、qq和微博等
															if (~res.provider.indexOf('weixin')) {
																console.log('weixin123')
																uni.login({
																	provider: 'weixin',
																	success: (loginRes)=> {
																		console.log('-------获取openid(unionid)-----');
																		console.log(loginRes);
																		console.log(JSON.stringify(loginRes));
																		// 获取用户信息
																		uni.getUserInfo({
																			provider: 'weixin',
																			success: async (infoRes)=> {
																				uni.showLoading({title: '加载中'})
																				console.log('-------获取微信用户所有-----');
																				console.log(infoRes.userInfo);
																				uni.setStorageSync('userInfo', infoRes.userInfo)
																				let baseUrl = ''
																				if (process.env.NODE_ENV === "development") {
																					// baseUrl = 'http://192.168.31.83:20100'
																					baseUrl = 'https://api.arapp.net'
																				} else {
																					baseUrl = 'https://api.arapp.net'
																				}
																				uni.request({
																					url: baseUrl + '/client/visitor/auth',
																					data: {openid: infoRes.userInfo.openId,unionid: infoRes.userInfo.unionId},
																					success: (res) => {
																						console.log(res.data)
																						if (res.data.code === 207) {
																							uni.showToast({title: res.data.message, icon: 'none'})
																							uni.navigateTo({url: '/pages/setting/bind-phone'})
																						} else if (res.data.code === 0) {
																							// #ifdef APP-PLUS
																							this.$IM.login({userId:res.data.data.member.id + '',userSig:res.data.data.sin},(ret)=>{
																								console.log('IM登录',ret);
																							});
																							// #endif
																							uni.setStorageSync('user',res.data.data)
																							uni.setStorageSync('token',res.data.data.token)
																							uni.setStorageSync('isLogin', true)
																							uni.setStorageSync('isRefresh', true)
																							uni.startPullDownRefresh()
																							
																						} else {
																							uni.showToast({title: res.data.message, icon: 'none'})
																						}
																					},
																					fail: (err) => {
																						console.log(err)
																					},
																					complete: () => {
																						uni.hideLoading()
																					}
																				})
																			}
																		});
																	}
																});
															}
														}
													})
												})
												
											}
										})
										console.log('ios')
										jv.setCustomUIWithConfigiOS({
											privacyState: true,
											numberSize: 24,
											// navColor: 0xff000000,
											logBtnText: " 一键登录 ",
											logoImg: "static/login_an.png",
											logoWidth: 100,
											logoHeight: 100,
											//slogn 中国移动提供认证服务
											sloganConstraints:[0,-75,200,14],
											sloganTextColor: -10066330,
											logBtnImgs: ["static/quick_login.png","static/quick_login_forbid","static/quick_login.png"],
											logBtnConstraints: [0,-20,315,45],
											appPrivacyColor: [-10066330,-10439065],
											// checkViewConstraints:[-54,200,12,12],
											addCustomViews: [
												{
												   type:"button",
												   width:89,
												   height:40,
												   top:320,
												   left:duanLeft || 77,
												   id: "1",
												   title:"短信验证码登录",
												   textFont:12,
												   textAlignment:15,
												   numberOfLines:2,
												   cornerRadius:10,
												   textColor:-10066330
												},
												{
												   type:"button",
												   width:220,
												   height:40,
												   top:420,
												   left:80,
												   id:"2",
												   title:"",
												   textFont:12,
												   textAlignment:15,
												   numberOfLines:2,
												   cornerRadius:10,
												   textColor:0xff000000
												},
												// {
												//    type:"button",
												//    width: width || 325,
												//    height:65,
												//    top:430,
												//    left:left || 25,
												//    id: "3",
												//    cornerRadius:10,
												//    normalImagePath:"static/quick_login_wx.png"
												// }
											]
										})
									} else {
										jv.addCustomViewsClickCallback(id=>{
											console.log(id)
											if (id == 1) {
												// 隐藏登录页
												jv.dismissLoginAuth(false, res=>{
													console.log(123445);
													//授权页隐藏完成
													uni.navigateTo({url: '/pages/login/login'})
												})
											} else if (id == 2) {
												jv.dismissLoginAuth(false, res=>{
													uni.startPullDownRefresh()
												})
											} else if (id == 3) {
												jv.dismissLoginAuth(false, res=>{
													uni.getProvider({
														service: 'oauth',
														success: (res)=> {
															console.log(res.provider);
															//支持微信、qq和微博等
															if (~res.provider.indexOf('weixin')) {
																console.log('weixin123')
																uni.login({
																	provider: 'weixin',
																	success: (loginRes)=> {
																		console.log('-------获取openid(unionid)-----');
																		console.log(loginRes);
																		console.log(JSON.stringify(loginRes));
																		// 获取用户信息
																		uni.getUserInfo({
																			provider: 'weixin',
																			success: async (infoRes)=> {
																				uni.showLoading({title: '加载中'})
																				console.log('-------获取微信用户所有-----');
																				console.log(infoRes.userInfo);
																				uni.setStorageSync('userInfo', infoRes.userInfo)
																				let baseUrl = ''
																				if (process.env.NODE_ENV === "development") {
																					// baseUrl = 'http://192.168.31.83:20100'
																					baseUrl = 'https://api.arapp.net'
																				} else {
																					baseUrl = 'https://api.arapp.net'
																				}
																				uni.request({
																					url: baseUrl + '/client/visitor/auth',
																					data: {openid: infoRes.userInfo.openId,unionid: infoRes.userInfo.unionId},
																					success: (res) => {
																						console.log(res.data)
																						if (res.data.code === 207) {
																							uni.showToast({title: res.data.message, icon: 'none'})
																							uni.navigateTo({url: '/pages/setting/bind-phone'})
																							
																						} else if (res.data.code === 0) {
																							// #ifdef APP-PLUS
																							this.$IM.login({userId:res.data.data.member.id + '',userSig:res.data.data.sin},(ret)=>{
																								console.log('IM登录',ret);
																							});
																							// #endif
																							uni.setStorageSync('user',res.data.data)
																							uni.setStorageSync('token',res.data.data.token)
																							uni.setStorageSync('isLogin', true)
																							uni.setStorageSync('isRefresh', true)
																							uni.startPullDownRefresh()
																						} else {
																							uni.showToast({title: res.data.message, icon: 'none'})
																						}
																					},
																					fail: (err) => {
																						console.log(err)
																					},
																					complete: () => {
																						uni.hideLoading()
																					}
																				})
																			}
																		});
																	}
																});
															}
														}
													})
												})
											}
										})
										// 自定义页面
										jv.setCustomUIWithConfigAndroid({
											setPrivacyState: true,
											setLogoWidth: 100,
											setLogoHeight: 100,
											setNavColor:0xff000000,
											setNumberColor:-15132391,
											setNumberSize:22,
											setLogBtnWidth: 315,
											setLogBtnHeight: 45,
											setLogBtnText:" 一键登录 ",
											setLogBtnImgPathFromJs:"static/quick_login.png",
											setLogoImgPathFromJs:"static/login_an.png",
											addCustomViews: [
												{
												   type:"text",
												   finishFlag:false,
												   id:"1",
												   // width:duanWidth || 220,
												   width:89,
												   height:40,
												   text:"短信验证码登录",
												   textSize:12,
												   // margins:[duanLeft||77,320,0,0]
												   margins:[duanLeft||77,320,0,0]
												},
												{
												   type:"text",
												   finishFlag:false,
												   id:"2",
												   width:180,
												   height:50,
												   text:"",
												   textSize:12,
												   margins:[143,410,0,0]
												},
												// {
												//    type:"image",
												//    finishFlag:false,
												//    id:"3",
												//    width: width || 325,
												//    height:65,
												//    margins:[left||25,430,0,0],
												//    bgImgPath: 'static/quick_login_wx.png',
												//    cornerRadius: "40"
												// }
											]
										})
									}
									jv.loginAuth({
										autoFinish:true,
										timeout:5000
									},async result=>{
										// 结果监听
										let code = result.code;
										let loginToken = result.content;
										let operator = result.operator;
										console.log(result)
										if (code === 6002) {
											let routes = getCurrentPages(); // 获取当前打开过的页面路由数组
											let curRoute = routes[routes.length - 1].route // 获取当前页面路由
											console.log(curRoute)
											if (curRoute.includes('login')) {
												// uni.navigateBack()
												uni.navigateTo({
													url:'pages/login/login'
												})
											}
											resolve('cancel')
										} else {
											let baseUrl = ''
											if (process.env.NODE_ENV === "development") {
												// baseUrl = 'http://192.168.31.83:20100'
												baseUrl = 'https://api.arapp.net'
											} else {
												baseUrl = 'https://api.arapp.net'
											}
											uni.request({
												url: baseUrl + '/client/visitor/loginToken',
												data: {token: loginToken},
												success: (res) => {
													console.log(res.data)
													// #ifdef APP-PLUS
													this.$IM.login({userId:res.data.data.member.id + '',userSig:res.data.data.sin})
													// #endif
													uni.setStorageSync('user',res.data.data)
													uni.setStorageSync('token',res.data.data.token)
													uni.setStorageSync('isLogin', true)
													uni.setStorageSync('isRefresh', true)
													resolve('登录成功' + res.data.data.token)
													let routes = getCurrentPages()
													let curRoute = routes[routes.length - 1].route
													console.log(curRoute)
													if (curRoute==="pages/user/user") {
														uni.startPullDownRefresh()
													}
													// uni.startPullDownRefresh()
													// uni.setStorage({
													// 	key: 'token', 
													// 	data: res.data.data.token, 
													// 	success: ()=> {
													// 		console.log('token设置登录成功')
													// 		// uni.redirectTo({url: '/pages/index/index2.vue'})
													// 		resolve('登录成功' + res.data.data.token)
													// 		let routes = getCurrentPages()
													// 		let curRoute = routes[routes.length - 1].route
													// 		console.log(curRoute)
													// 		if (curRoute==="pages/user/user") {
													// 			// uni.setStorageSync({
													// 			//     key: 'isLogin',
													// 			//     data: true,
													// 			//     success: ()=> {
													// 			// 		console.log('登录成功')
													// 			// 		uni.startPullDownRefresh()
													// 			//     }
													// 			// })
													// 			// uni.setStorageSync('isLogin', true)
													// 			// uni.setStorageSync('isRefresh', true)
													// 			uni.startPullDownRefresh()
													// 		}
															
													// 	}
													// })
												},
												fail: (err) => {
													console.log(err)
												}
											})
										}
									},event=>{
										// 事件监听
										let code = event.code;
										let eventDesc = event.content;
										console.log(event)
									})
								} else {
									resolve('fail')
									// reject('一键登录失败')
									// uni.navigateTo({url: '/pages/login/login'})
								}
							})
						}
					})
				}
			})
			
		});
	
	})
	// #endif
}